package com.yanqu.road.dao.impl.activity.commonwish;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.commonwish.CommonWishUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CommonWishDataDaoImpl extends TempDataObjectDao<CommonWishUserData> {

    public Map<Integer, CommonWishUserData> getCommonWishUserData(List<Integer> activityIdList, long userId) {
        String sql = "select * from `t_u_common_wish_data` where `activityId` in (" + StringUtils.listToString(activityIdList, ",") +") and `userId`=?";
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, CommonWishUserData> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                CommonWishUserData data = parse(rs);
                result.put(data.getActivityId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String getCommonWishUserDataSql = "select * from `t_u_common_wish_data` where `activityId`=? and `userId`=?";
    public CommonWishUserData getCommonWishUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getCommonWishUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateCommonWishUserDataSql = "update `t_u_common_wish_data` set positionList=?,allDrawOpp=?,drawOpp=?,bigSelectMap=?,score=?,bigReward=?,resetTime=?,drawRound=?,drawTimes=?,totalDrawTimes=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(CommonWishUserData userFanXiuSpringData) {
        userFanXiuSpringData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFanXiuSpringData.getPositionList())));
        para.put(i++, new DbParameter(Types.INTEGER, userFanXiuSpringData.getAllDrawOpp()));
        para.put(i++, new DbParameter(Types.INTEGER, userFanXiuSpringData.getDrawOpp()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFanXiuSpringData.getBigSelectMap())));
        para.put(i++, new DbParameter(Types.INTEGER, userFanXiuSpringData.getScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, userFanXiuSpringData.getBigReward()));
        para.put(i++, new DbParameter(Types.INTEGER, userFanXiuSpringData.getResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userFanXiuSpringData.getDrawRound()));
        para.put(i++, new DbParameter(Types.INTEGER, userFanXiuSpringData.getDrawTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, userFanXiuSpringData.getTotalDrawTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, userFanXiuSpringData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userFanXiuSpringData.getUserId()));
        if (getDb().executeNoneQuery(updateCommonWishUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCommonWishUserDataSql = "insert into `t_u_common_wish_data`(activityId,userId,positionList,allDrawOpp,drawOpp,bigSelectMap,score,bigReward,resetTime,drawRound,drawTimes,totalDrawTimes) values (?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CommonWishUserData userFanXiuSpringData) {
        userFanXiuSpringData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userFanXiuSpringData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userFanXiuSpringData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFanXiuSpringData.getPositionList())));
        para.put(i++, new DbParameter(Types.INTEGER, userFanXiuSpringData.getAllDrawOpp()));
        para.put(i++, new DbParameter(Types.INTEGER, userFanXiuSpringData.getDrawOpp()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFanXiuSpringData.getBigSelectMap())));
        para.put(i++, new DbParameter(Types.INTEGER, userFanXiuSpringData.getScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, userFanXiuSpringData.getBigReward()));
        para.put(i++, new DbParameter(Types.INTEGER, userFanXiuSpringData.getResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userFanXiuSpringData.getDrawRound()));
        para.put(i++, new DbParameter(Types.INTEGER, userFanXiuSpringData.getDrawTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, userFanXiuSpringData.getTotalDrawTimes()));
        if (getDb().executeNoneQuery(insertCommonWishUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CommonWishUserData parseData(ResultSet rs) throws SQLException {
        CommonWishUserData userFanXiuSpringData = new CommonWishUserData();
        userFanXiuSpringData.setActivityId(rs.getInt("activityId"));
        userFanXiuSpringData.setUserId(rs.getLong("userId"));
        userFanXiuSpringData.setPositionList(JSON.parseObject(rs.getString("positionList"), new TypeReference<List<Integer>>() {}));
        userFanXiuSpringData.setAllDrawOpp(rs.getInt("allDrawOpp"));
        userFanXiuSpringData.setDrawOpp(rs.getInt("drawOpp"));
        userFanXiuSpringData.setBigSelectMap(JSON.parseObject(rs.getString("bigSelectMap"), new TypeReference<Map<Integer, Integer>>() {}));
        userFanXiuSpringData.setScore(rs.getInt("score"));
        userFanXiuSpringData.setBigReward(rs.getString("bigReward"));
        userFanXiuSpringData.setResetTime(rs.getInt("resetTime"));
        userFanXiuSpringData.setDrawRound(rs.getInt("drawRound"));
        userFanXiuSpringData.setDrawTimes(rs.getInt("drawTimes"));
        userFanXiuSpringData.setTotalDrawTimes(rs.getInt("totalDrawTimes"));
        return userFanXiuSpringData;
    }
}
